<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="税源地名称" prop="companyName" label-width="120px">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入税源地名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="管理员名称" prop="administratorName">
        <el-input
          v-model="queryParams.administratorName"
          placeholder="请输入管理员名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="认证状态" prop="authenticationStatus">
        <el-select v-model="queryParams.authenticationStatus" clearable>
          <el-option label="未认证" :value="0"></el-option>
          <el-option label="已认证" :value="1"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:tax_territory:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:tax_territory:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="taxTerritoryList">
      <el-table-column label="税源地名称" align="center" prop="companyName" min-width="200">
        <template slot-scope="{ row }">
          <div v-risk="row.riskLevel">
            {{row.companyName}}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="管理员名称" align="center" prop="administratorName" min-width="150"/>
      <el-table-column label="管理员手机号" align="center" prop="administratorPhone" min-width="160"/>
      <el-table-column label="法人名称" align="center" prop="legalRepresentativeName" min-width="150"/>
      <el-table-column label="认证状态" align="center" prop="authenticationStatus" min-width="120">
        <template slot-scope="{ row }">
          <el-tag v-if="row.authenticationStatus === 1">已认证</el-tag>
          <el-tag v-else type="warning">未认证</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" min-width="180"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="160">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-more-outline"
            @click="handleContract(scope.row)"
            v-if="scope.row.authenticationStatus === 1"
            v-hasPermi="['system:tax_territory:authentication']"
          >合同管理</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-more-outline"
            @click="handleAuthentication(scope.row)"
            v-else-if="scope.row.authenticationStatus === 0"
            v-hasPermi="['system:tax_territory:authentication']"
          >立即认证</el-button>

          <el-dropdown @command="(command)=>handleCommand(command,scope.row)">
            <el-button size="mini" type="text">更多<i class="el-icon-arrow-down el-icon--right"></i></el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="detail" v-has-permi="['system:tax_territory:query']">详情</el-dropdown-item>
              <el-dropdown-item command="edit" v-has-permi="['system:tax_territory:edit']">修改基本信息</el-dropdown-item>
              <el-dropdown-item command="edit_settlement" v-has-permi="['system:tax_territory:edit']">修改其他信息</el-dropdown-item>
              <el-dropdown-item command="resetPwd" v-hasPermi="['system:agent:edit']">重置密码</el-dropdown-item>
              <el-dropdown-item command="change" v-has-permi="['system:organizationInfoChange:list']">变更记录</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
      <template slot="empty">
        <el-empty :image-size="100"></el-empty>
      </template>
    </el-table>

    <el-drawer :wrapperClosable="false" :close-on-press-escape="false" @close="form.organizationId = null" destroy-on-close :title="title" :visible.sync="open" size="55%" class="app-drawer" append-to-body>
      <tax-territory-operate @cancel="handleCancel" :organizationId="form.organizationId"></tax-territory-operate>
    </el-drawer>

    <el-drawer :wrapperClosable="false" :close-on-press-escape="false" @close="form.organizationId = null" destroy-on-close title="税源地详情" :visible.sync="detailOpen" size="55%" class="app-drawer" append-to-body>
      <tax-territory-detail @cancel="handleDetailCancel" :organizationId="form.organizationId"></tax-territory-detail>
    </el-drawer>

    <el-drawer :wrapperClosable="false" :close-on-press-escape="false" @close="form.organizationId = null" destroy-on-close title="修改其他信息" :visible.sync="detailSettlementOpen" size="55%" class="app-drawer" append-to-body>
      <tax-territory-settlement @cancel="handleDetailCancel" :organizationId="form.organizationId"></tax-territory-settlement>
    </el-drawer>

    <el-drawer :wrapperClosable="false" :close-on-press-escape="false" @close="form.organizationId = null" destroy-on-close title="变更记录" :visible.sync="changeOpen" size="55%" class="app-drawer" append-to-body>
      <organization-change @cancel="handleDetailCancel" :organizationId="form.organizationId"></organization-change>
    </el-drawer>

    <el-drawer :wrapperClosable="false" :close-on-press-escape="false"  title="合同管理" :visible.sync="contractOpen" size="65%" destroy-on-close append-to-body>
      <contract-info v-if="contractOpen" :type="0" :organizationId="form.organizationId"></contract-info>
    </el-drawer>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import ContractInfo from "@/views/contract/contractInfo/index.vue";
import TaxTerritoryDetail from "@/views/system/tax_territory/profile/detail.vue";
import TaxTerritoryOperate from "@/views/system/tax_territory/profile/operate.vue";
import TaxTerritorySettlement from "@/views/system/tax_territory/profile/settlementInfo.vue";
import OrganizationChange from "@/views/system/tax_territory/profile/organizationInfoChange.vue";
import { listTaxTerritory, getAuthenticationUrl, delTaxTerritory, resetUserPwd } from "@/api/system/tax_territory"

export default {
  name: "tax_territory",
  components: {
    ContractInfo, TaxTerritorySettlement, TaxTerritoryDetail, TaxTerritoryOperate, OrganizationChange},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业信息表格数据
      taxTerritoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      detailOpen:false,
      changeOpen:false,
      contractOpen:false,
      detailSettlementOpen:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        authenticationStatus:null,
        companyName: null,
        administratorName: null
      },
      // 表单参数
      form: {
        organizationId:null
      },

    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询企业信息列表 */
    getList() {
      this.loading = true
      listTaxTerritory(this.queryParams).then(response => {
        this.taxTerritoryList = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.open = true
      this.title = "新增税源地"
    },
    /** 修改按钮操作 */
    handleCommand(command,row) {
      this.form.organizationId = row.organizationId
      if (command === "edit"){
        this.open = true
        this.title = "修改税源地"
      }else if (command === "detail") {
        this.detailOpen = true
      }else if (command === "edit_settlement"){
        this.detailSettlementOpen = true
      }else if (command === "change"){
        this.changeOpen = true
      }else if (command === "resetPwd"){
        this.handleResetPwd(row)
      }
    },

    /** 重置密码按钮操作 */
    handleResetPwd(row) {
      this.$prompt('请输入税源地名称"' + row.companyName + '"的新密码', "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        inputPattern: /^.{5,20}$/,
        inputErrorMessage: "用户密码长度必须介于 5 和 20 之间",
        inputValidator: (value) => {
          if (/<|>|"|'|\||\\/.test(value)) {
            return "不能包含非法字符：< > \" ' \\\ |"
          }
        },
      }).then(({ value }) => {
        resetUserPwd(row.organizationId, value).then(response => {
          this.$modal.msgSuccess("修改成功，新密码是：" + value)
        })
      }).catch(() => {})
    },

    handleAuthentication(row){
      this.loading = true
      getAuthenticationUrl(row.organizationId).then(res=>{
        this.loading = false
        if (res.code === 200){
          window.open(res.data)
        }
      }).catch(()=>{
        this.loading = false
      })
    },

    handleContract(row){
      this.form.organizationId = row.organizationId
      this.contractOpen = true
    },

    handleCancel(val){
      this.open = false
      if (val){
        this.getList()
      }
    },

    handleDetailCancel(){
      this.detailOpen = false
      this.detailSettlementOpen = false
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const organizationIds = row.organizationId || this.ids
      this.$modal.confirm('是否确认税源地为"' + row.nickName + '"的数据项？').then(function() {
        return delTaxTerritory(organizationIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/tax_territory/export', {
        ...this.queryParams
      }, `税源管理_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
